﻿Public Class frm_ABM_TipoCurso
    Dim string_conexion = "Data Source=JACKY-PC\SQLEXPRESS;Initial Catalog=bd_academia;Integrated Security=SSPI;Provider=SQLOLEDB"
    'Dim string_conexion As String = "Provider=SQLOLEDB;Data Source=NEHEMÍAS-NTBK\SQLSERVER2008;Integrated Security=SSPI;Initial Catalog=bd_academia"

    Dim _accesoBD As New AccesoBD
    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub

    Private Sub btnAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregar.Click
        Dim consulta As String = ""

        If Not Me.txtTipoCurso.Text = "" Then
            'le paso solamente el la descripcion del curso, porque el id es autoincremental
            consulta = "insert into TipoCurso values ('" & txtTipoCurso.Text & "')"
            _accesoBD._insertar(consulta)
            MessageBox.Show("Cargado con éxito", "Resultado actualizacion", MessageBoxButtons.OK)
            Me.txtTipoCurso.Text = ""
            cargar_grilla()
        Else
            MessageBox.Show("Debe ingresar una descripcion", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

    End Sub

    Private Sub cargar_grilla()
        Dim tabla As New DataTable
        Dim consulta As String = ""

        consulta = "select * from TipoCurso"

        tabla = _accesoBD.ejecutaSQL(consulta)

        Me.grillaTipoCurso.Rows.Clear()

        Dim c As Integer = 0

        For c = 0 To tabla.Rows.Count - 1
            Me.grillaTipoCurso.Rows.Add(tabla.Rows(c)("idTipoCurso"), tabla.Rows(c)("nombre"))
        Next
    End Sub

    Private Sub frm_ABM_TipoCurso_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cargar_grilla()
    End Sub

    Private Sub EliminarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EliminarToolStripMenuItem.Click
        Dim id As Integer
        Dim eliminar As String = ""
        id = Me.grillaTipoCurso.CurrentRow.Cells("col_id").Value

        If MsgBox("Esta seguro que desea eliminar el resgristro " & id & " ?!", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            eliminar = "delete from TipoCurso where idTipoCurso = " & id
            _accesoBD._eliminar(eliminar)
            cargar_grilla()
        End If
    End Sub
End Class